package com.svconnect.plugin.builders;

import java.io.IOException;
import java.util.List;

import com.svconnect.plugin.model.wsdl.Service;
import com.svconnect.plugin.readers.WSDLoader;
import com.svconnect.plugin.util.Log;

public class DiscoBuilder extends BuilderBase {

	private String discoUrl;
	
	public DiscoBuilder(String url, String referenceDir) {
		this.discoUrl = fixDiscoUrl(url);
		this.setReferenceDir(referenceDir);
	}
	
	private String fixDiscoUrl(String url) {
		return url.substring(0,url.lastIndexOf(".svc")+4)+"?disco";
	}
	
	public void build(List<Service> services) throws IOException {
		Log.i("Fetching "+discoUrl);
		String discoFile = WSDLoader.load(discoUrl);
		if (services!=null) {
			for(Service s : services) {
				String path = String.format("%s/%s/%s.disco", this.getReferenceDir(),s.getName(),s.getName());
				FileWriter fw = new FileWriter(path);
				fw.write(discoFile);
			}
		}
	}
	
	
	
}
